<?php
	$name = array(
		'name'	=> 'name',
		'id'	=> 'name',
		'class'	=> 'input ui-corner-all',
		'style' => 'width: 200px',
		'value' => set_value('name', $download->name),
	);
	
	$description = array(
		'name'	=> 'description',
		'id'	=> 'description',
		'class'	=> 'input ui-corner-all',
		'style' => 'height: 80px;',
		'value' => set_value('description', $download->description),
	);
	
	$file = array(
		'name'	=> 'file_upload',
		'id'	=> 'file_upload',
		'value' => set_value('file_upload'),
	);
	
	$file_name = array(
		'name'	=> 'file_name',
		'value' => set_value('file_name', $download->file_name),
	);
	
	$file_path = array(
		'name'	=> 'file_path',
		'value' => set_value('file_path', $download->file_path),
	);
	
	$file_size = array(
		'name'	=> 'file_size',
		'value' => set_value('file_size', $download->file_size),
	);
	
	$file_type = array(
		'name'	=> 'file_type',
		'value' => set_value('file_type', $download->file_type),
	);
?>
<div class="container_12" id="body">
	<div class="grid_12">
	<?php echo form_open_multipart('backend/download/create/', array('id' => 'form_download_create'));?>
		<div style="text-align: right;">
			<button id="btn_create">Lưu</button> 
			<?php echo anchor('backend/download', 'Hủy', 'class="button" id="btn_cancel"');?>
		</div>
		<?php if ($message): ?>
		<div style="margin: 20px auto; padding: 1em;" class="ui-state-highlight ui-corner-all"> 
			<p><span style="float: left; margin-right: 0.3em;" class="ui-icon ui-icon-info"></span>
			<strong>Hey!</strong> <?php echo $message; ?></p>
		</div>
		<div class="clear"></div>
		<?php endif;?>
		<div>
			<h4>Thông tin</h4>
			<div>
				<?php echo form_label('Tên file', 'name', array('style' => 'width: 120px')); ?>
				<?php echo form_input($name); ?>
				<div class="clear"></div>
			</div>
			<div class="b20">
				<?php echo form_label('Mô tả', 'description', array('style' => 'width: 120px')); ?>
				<?php echo form_textarea($description); ?>
				<div class="clear"></div>
			</div>
			<div class="b20">
				<?php echo form_label('Tải file', 'file_upload', array('style' => 'width: 120px')); ?>
				<?php echo form_upload($file); ?>
				<button id="btn_upload">Tải file</button> 
				<div class="clear"></div>
				<?php echo form_hidden($file_name['name'], $file_name['value']); ?>
				<?php echo form_hidden($file_path['name'], $file_path['value']); ?>
				<?php echo form_hidden($file_size['name'], $file_size['value']); ?>
				<?php echo form_hidden($file_type['name'], $file_name['value']); ?>
			</div>
			<ul id="file_info">
				<li>Tên file: <span id="file_name"><?php echo $download->file_name; ?></span></li>
				<li>Đường dẫn: <span id="file_path"><?php echo $download->file_path; ?></span></li>
				<li>Kích cỡ: <span id="file_size"><?php echo $download->file_size; ?></span></li>
				<li>Loại file: <span id="file_type"><?php echo $download->file_type; ?></span></li>
			</ul>
		</div>
		<div class="clear"></div>
	<?php echo form_close();?>
	</div>
	<div class="clear"></div>
</div>
<script type="text/javascript">
$(function() {
	$('#btn_upload').click(function() {
		if ($('#file_upload').val() != '') {
			$.ajaxFileUpload( {
				url : base_path + 'backend/download/create/upload_file',
				secureuri : false,
				fileElementId : 'file_upload',
				dataType : 'json',
				success : function(data, status) {
					if(data.error == false) {
						$('input[name="file_name"]').val(data.response.file_name);
						$('input[name="file_path"]').val(data.response.file_path);
						$('input[name="file_size"]').val(data.response.file_size);
						$('input[name="file_type"]').val(data.response.file_type);

						$('span#file_name').text(data.response.file_name);
						$('span#file_path').text(data.response.file_path);
						$('span#file_size').text(data.response.file_size);
						$('span#file_type').text(data.response.file_type);
						$('#file_info').show();
					} else {
						alert(data.response);
					}
				},
				error : function(data, status, e) {
					alert('Upload error : ' + e);
				}
			});
		}

		return false;
	});

	$('#btn_create').click(function() {
		if($('input[name="file_name"]').val().length == 0){
			alert('Hãy upload file trước!');
			return false;
		}
	});
});
</script>